#include <QLineEdit>
#include <QSplitter>
#include <QAction>
#include <QToolBar>
#include <QVBoxLayout>
#include <QDebug>
#include <QThread>
#include <QUrl>

#include "dirtree.h"
#include "imagegallery.h"
#include "filemanager.h"
#include "downloaddialog.h"
#include "../filedownloader.h"

namespace Jcr
{
class FileManagerPrivate
{
public:
    QString saveDir;
};

FileManager::FileManager(QWidget *parent) : QWidget(parent)
{
    d_ptr = new FileManagerPrivate;
    init();
}

FileManager::~FileManager()
{
    delete d_ptr;
}

QSize FileManager::sizeHint() const
{
    return QSize(800, 500);
}

void FileManager::init()
{
    QVBoxLayout* mainLayout = new QVBoxLayout;
    QSplitter* splitter = new QSplitter(Qt::Horizontal, this);

    QToolBar* toolbar = new QToolBar(tr("File Manager"), this);
    mainLayout->addWidget(toolbar);
    mainLayout->addWidget(splitter);

    QAction* act = createAction(tr("Add Download"), SLOT(addDownload()));
    toolbar->addAction(act);

    act = createAction(tr("Move Images"), SLOT(moveImages()));
    toolbar->addAction(act);

    act = createAction(tr("Select All"), SLOT(selectAll()));
    act->setCheckable(true);
    toolbar->addAction(act);

    act = createAction(tr("Edit"), SLOT(editImages()));
    act->setCheckable(true);
    toolbar->addAction(act);

    act = createAction(tr("Delete"), SLOT(deleteImages()));
    toolbar->addAction(act);
    toolbar->addSeparator();

    act = createAction(tr("Set as Detailed"), SLOT(setAsDetailedImages()));
    toolbar->addAction(act);

    act = createAction(tr("Set as Small"), SLOT(setAsSmallImages()));
    toolbar->addAction(act);

    act = createAction(tr("Set as Outdoor"), SLOT(setAsOutdoorImages()));
    toolbar->addAction(act);

    QWidget* dirWidget = new QWidget(this);
    dirWidget->setMaximumWidth(400);
    QVBoxLayout* dirWidgetLayout = new QVBoxLayout;
//    dirWidgetLayout->setSpacing(1);
    m_searchEdit = new QLineEdit(dirWidget);
    m_searchEdit->setClearButtonEnabled(true);
    m_searchEdit->setPlaceholderText(tr("Search"));
    m_dirTree = new Jcr::DirTree(dirWidget);
    dirWidgetLayout->addWidget(m_searchEdit);
    dirWidgetLayout->addWidget(m_dirTree);
    dirWidget->setLayout(dirWidgetLayout);
    splitter->addWidget(dirWidget);

    m_imageGallery = new Jcr::ImageGallery(this);
    splitter->addWidget(m_imageGallery);

    connect(m_dirTree, SIGNAL(load(QString)), m_imageGallery, SLOT(load(QString)));
    connect(m_dirTree, SIGNAL(download(QUrl)), this, SLOT(addDownload(QUrl)));
    connect(m_dirTree, SIGNAL(notify(QString)), this, SIGNAL(notify(QString)));
    connect(m_dirTree, SIGNAL(modelChanged(QString)), this, SIGNAL(modelChanged(QString)));
    connect(m_searchEdit, SIGNAL(textChanged(QString)), this, SLOT(onSearchTextChanged(QString)));
    connect(m_imageGallery, SIGNAL(notify(QString)), this, SIGNAL(notify(QString)));

    setLayout(mainLayout);
    setWindowTitle(tr("File Manager"));
}

QAction* FileManager::createAction(const QString& text, const QIcon& icon, const char* slot)
{
    QAction* act = new QAction(icon, text, this);
    connect(act, SIGNAL(triggered()), this, slot);
    return act;
}

QAction* FileManager::createAction(const QString& text, const char* slot)
{
    QAction* act = new QAction(text, this);
    connect(act, SIGNAL(triggered()), this, slot);
    return act;
}

void FileManager::addDownload()
{
    addDownload(QUrl());
}

void FileManager::addDownload(const QUrl &url)
{
    Jcr::DownloadDialog dd;
    dd.setWindowFlags(Qt::WindowStaysOnTopHint);
    dd.setUrl(url.toString());

//    if (d_ptr->saveDir.isEmpty())
        dd.setSaveDir(m_dirTree->currentDir());
//    else
//        dd.setSaveDir(d_ptr->saveDir);

    int ret = dd.exec();
    if (ret == QDialog::Accepted) {
        if (!dd.url().isEmpty()) {
            d_ptr->saveDir = dd.saveDir();
            emit download(QUrl(dd.url()), dd.saveName(), dd.saveDir());
#if 0
            Jcr::FileDownloader* fd = new Jcr::FileDownloader(QUrl(dd.url()));
            fd->setSaveDir(dd.saveDir());
            fd->setSaveName(dd.saveName());

            QThread* thread = new QThread;
            fd->moveToThread(thread);

            connect(fd, SIGNAL(finished()), this, SLOT(onFileDownloaded()));
            connect(thread, SIGNAL(started()), fd, SLOT(start()));
            connect(fd, SIGNAL(finished()), thread, SLOT(terminate()));
            connect(fd, SIGNAL(finished()), thread, SLOT(deleteLater()));

            thread->start();
#endif
        }
    }
}

void FileManager::moveImages()
{

}

void FileManager::selectAll()
{
    QAction* act = qobject_cast<QAction*>(sender());
    m_imageGallery->selectAll(act->isChecked());
}

void FileManager::editImages()
{
    QAction* act = qobject_cast<QAction*>(sender());
    if (act->isChecked())
        m_imageGallery->edit();
    else
        m_imageGallery->view();
}

void FileManager::viewImages()
{
    m_imageGallery->view();
}

void FileManager::deleteImages()
{

}

void FileManager::setAsDetailedImages()
{
    m_imageGallery->setAsDetailed();
}

void FileManager::setAsSmallImages()
{
    m_imageGallery->setAsSmall();
}

void FileManager::setAsOutdoorImages()
{
    m_imageGallery->setAsOutdoor();
}

void FileManager::onFileDownloaded()
{
    Jcr::FileDownloader* fd = qobject_cast<Jcr::FileDownloader*>(sender());
    m_imageGallery->addImage(fd->localPath());
    fd->deleteLater();
}

void FileManager::onSearchTextChanged(const QString &cond)
{
    bool ok = m_dirTree->find(cond);
    if (ok) {
        m_searchEdit->setStyleSheet("color:black;");
    } else {
        m_searchEdit->setStyleSheet("color:red;");
    }
}

} // namespace jcr
